/*
 * @Author: sunli
 * @Date: 2024-02-21 10:35:21
 * @LastEditors: sunli
 * @LastEditTime: 2024-02-21 20:07:00
 * @FilePath: /debug-server/router/errorReport.js
 * @Description: 
 */
let express = require('express')
let router = express()
let mongoose = require('mongoose')
let errorModel = mongoose.model('errorReport')
let projectModel = mongoose.model('project')

// 上传报告
router.post('/errorUpload', async (req, res) => {
  const body = req.body
  const data = new errorModel({
    _id: new mongoose.Types.ObjectId(),
    KEY: body.KEY,
    title: body.title, // 页面标题
    url: body.url, // 页面URL
    timestamp: body.timestamp, // 访问时间戳
    userAgent: body.userAgent, // 用户浏览器类型
    kind: body.kind, // 大类
    type: body.type, // 小类
    errorType: body.errorType, // 错误类型
    message: body.message, // 类型详情
    filename: body.filename, // 访问的文件名
    position: body.position, // 行列信息
    stack: body.stack, // 堆栈信息
    selector: body.selector, // 选择器

    // * promiseError

    //  * resourceError
    tagName: body.tagName, //标签名
  })

  data.save()
    .then(async () => {
      res.send({
        code: 1,
        message: '错误报告上传成功',
      })
    })
    .catch(error => {
      res.send({
        code: 0,
        message: '错误报告上传失败'
      })
    })
})

// 项目错误列表
router.post('/projectError', async (req, res) => {
  const body = req.body

  const result = await errorModel.find({ KEY: body.KEY })

  res.send({
    code: 1,
    message: '查询成功',
    resObject: result || []
  })
})

// 错误详情
router.post('/queryErrorDaetail', async (req, res) => {
  const body = req.body
  const result = await errorModel.findOne({_id: body._id})

  res.send({
    code: 1,
    message: '查询成功',
    resObject: result
  })
})


module.exports = router
